<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Get info about given date formatted according to the specified format</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.date-offset-get.html">« date_offset_get</a></li>
      <li style="float: right;"><a href="function.date-parse.html">date_parse »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.datetime.html">Date/Time 函数</a></li>
    <li>Get info about given date formatted according to the specified format</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.date-parse-from-format" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">date_parse_from_format</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">date_parse_from_format</span> &mdash; <span class="dc-title">Get info about given date formatted according to the specified format</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.date-parse-from-format-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>date_parse_from_format</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$datetime</code></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   Returns associative array with detailed info about given date/time.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.date-parse-from-format-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">format</code></dt>

     <dd>

      <p class="para">
       Documentation on how the <code class="parameter">format</code> is used, please
       refer to the documentation of
       <span class="function"><a href="datetimeimmutable.createfromformat.html" class="function">DateTimeImmutable::createFromFormat()</a></span>. The same
       rules apply.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">datetime</code></dt>

     <dd>

      <p class="para">
       String representing the date/time.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.date-parse-from-format-returnvalues">
  <h3 class="title">返回值</h3>
  
  <p class="para">
   Returns associative array with detailed info about given date/time.
  </p>
  <p class="para">
   The returned array has keys for <code class="literal">year</code>,
   <code class="literal">month</code>, <code class="literal">day</code>, <code class="literal">hour</code>,
   <code class="literal">minute</code>, <code class="literal">second</code>,
   <code class="literal">fraction</code>, and <code class="literal">is_localtime</code>.
  </p>
  <p class="para">
   If <code class="literal">is_localtime</code> is present then
   <code class="literal">zone_type</code> indicates the type of timezone. For type
   <code class="literal">1</code> (UTC offset) the <code class="literal">zone</code>,
   <code class="literal">is_dst</code> fields are added; for type <code class="literal">2</code>
   (abbreviation) the fields <code class="literal">tz_abbr</code>,
   <code class="literal">is_dst</code> are added; and for type <code class="literal">3</code>
   (timezone identifier) the <code class="literal">tz_abbr</code>,
   <code class="literal">tz_id</code> are added.
  </p>
  <p class="para">
   The array includes <code class="literal">warning_count</code> and
   <code class="literal">warnings</code> fields. The first one indicate how many
   warnings there were.
   The keys of elements <code class="literal">warnings</code> array indicate the
   position in the given <code class="parameter">datetime</code> where the warning
   occurred, with the string value describing the warning itself. An example
   below shows such a warning.
  </p>
  <p class="para">
   The array also contains <code class="literal">error_count</code> and
   <code class="literal">errors</code> fields. The first one indicate how many errors
   were found.
   The keys of elements <code class="literal">errors</code> array indicate the
   position in the given <code class="parameter">datetime</code> where the error
   occurred, with the string value describing the error itself. An example
   below shows such an error.
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    The number of array elements in the <code class="literal">warnings</code> and
    <code class="literal">errors</code> arrays might be less than
    <code class="literal">warning_count</code> or <code class="literal">error_count</code> if they
    occurred at the same position.
   </p>
  </div>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.date-parse-from-format-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>7.2.0</td>
      <td>
       The <code class="literal">zone</code> element of the returned array represents
       seconds instead of minutes now, and its sign is inverted. For instance
       <code class="literal">-120</code> is now <code class="literal">7200</code>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.date-parse-from-format-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2073">
    <p><strong>示例 #1 <span class="function"><strong>date_parse_from_format()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"6.1.2009&nbsp;13:00+01:00"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">date_parse_from_format</span><span style="color: #007700">(</span><span style="color: #DD0000">"j.n.Y&nbsp;H:iP"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [year] =&gt; 2009
    [month] =&gt; 1
    [day] =&gt; 6
    [hour] =&gt; 13
    [minute] =&gt; 0
    [second] =&gt; 0
    [fraction] =&gt;
    [warning_count] =&gt; 0
    [warnings] =&gt; Array
        (
        )

    [error_count] =&gt; 0
    [errors] =&gt; Array
        (
        )

    [is_localtime] =&gt; 1
    [zone_type] =&gt; 1
    [zone] =&gt; 3600
    [is_dst] =&gt;
)
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2074">
    <p><strong>示例 #2 <span class="function"><strong>date_parse_from_format()</strong></span> with warnings example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"26&nbsp;August&nbsp;2022&nbsp;22:30&nbsp;pm"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$parsed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_parse_from_format</span><span style="color: #007700">(</span><span style="color: #DD0000">"j&nbsp;F&nbsp;Y&nbsp;G:i&nbsp;a"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Warnings&nbsp;count:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$parsed</span><span style="color: #007700">[</span><span style="color: #DD0000">'warning_count'</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$parsed</span><span style="color: #007700">[</span><span style="color: #DD0000">'warnings'</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$position&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\tOn&nbsp;position&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$position</span><span style="color: #007700">}</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$message</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Warnings count: 1
	On position 23: The parsed time was invalid
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2075">
    <p><strong>示例 #3 <span class="function"><strong>date_parse_from_format()</strong></span> with errors example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"26&nbsp;August&nbsp;2022&nbsp;CEST"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$parsed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_parse_from_format</span><span style="color: #007700">(</span><span style="color: #DD0000">"j&nbsp;F&nbsp;Y&nbsp;H:i"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Errors&nbsp;count:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$parsed</span><span style="color: #007700">[</span><span style="color: #DD0000">'error_count'</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$parsed</span><span style="color: #007700">[</span><span style="color: #DD0000">'errors'</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$position&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\tOn&nbsp;position&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$position</span><span style="color: #007700">}</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$message</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Errors count: 3
	On position 15: A two digit hour could not be found
	On position 19: Data missing
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.date-parse-from-format-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="datetimeimmutable.createfromformat.html" class="function" rel="rdfs-seeAlso">DateTimeImmutable::createFromFormat()</a> - Parses a time string according to a specified format</span></li>
    <li class="member"><span class="function"><a href="function.checkdate.html" class="function" rel="rdfs-seeAlso">checkdate()</a> - 验证一个格里高里日期</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>